<?php
namespace QuestionPlus\Service\Import;

use QuestionPlus\Service\Import\QuestionImport\QuestionImportFactory;

class Import
{
	private $importType;

	private $target;

	private $blockFlag = array(
		'question' => '【题目部分】',
		'answer' => '【答案部分】'
	);

	protected $types = array(
		'single_choice' => '【单项选择题】',
		'choice' => '【多项选择题】',
		'uncertain_choice' => '【不定项选择题】',
		'fill' => '【填空题】',
		'determine' => '【判断题】',
		'essay' => '【问答题】',
		'material' => '【材料题】'
	);

	public function __construct ($target, $importType)
    {
        $this->importType = $importType;
        $this->target = $target;
    }

    public function getQuestions ($path)
    {
    	$text = $this->checkCharset(file_get_contents($path));

    	$method = 'get' . ucfirst($this->importType) . 'Questions';
    	return $this->$method($text);
    }

    public function getSubQuestions($text)
    {
    	$method = 'get' . ucfirst($this->importType) . 'SubQuestions';
    	return $this->$method($text);
    }

    public function getSubAnswers($text)
    {
    	$method = 'get' . ucfirst($this->importType) . 'SubAnswers';
    	return $this->$method($text);
    }

	protected function getMixQuestions($text)
	{
		$typesStr = implode('|', $this->types);
		$questions = array();

		foreach ($this->types as $key => $type) {

			$question = $this->matchQuestionsByType($text, $type, false);

			if (empty($question)) {
				continue;
			}

			$questions[$key] = QuestionImportFactory::create($this->target, $this->importType, $key)->getQuestions($question);
		}

		return $questions;
	}

	protected function getMixSubQuestions($text)
	{
		$typesStr = implode('|', $this->types);
		$questions = array();

		foreach ($this->types as $key => $type) {

			$question = $this->matchQuestionsByType($text, $type, true);

			if (empty($question)) {
				continue;
			}

			$subQuestions = QuestionImportFactory::create($this->target, $this->importType, $key)->getSubQuestions($question);

			$questions = array_merge($questions, $subQuestions);
		}

		return $questions;
	}

	protected function getBlockQuestions($text)
	{
		$blockFlag = implode('|', $this->blockFlag);
		$blockTexts = preg_split('#'.$blockFlag.'#', $text, -1, PREG_SPLIT_NO_EMPTY);

		$typesStr = implode('|', $this->types);

		foreach ($this->types as $key => $type) {

			$question = $this->matchQuestionsByType($text, $type, false);

			if (empty($question)) {
				continue;
			}

			QuestionImportFactory::create($this->target, $this->importType, $key)->saveAnswers($answerText);
		}

		return $this->getMixQuestions($blockTexts[0]);
	}

	protected function getBlockSubQuestions($text)
	{
		return $this->getMixSubQuestions($text);
	}

	protected function getBlockSubAnswers($text)
	{
		$typesStr = implode('|', $this->types);
		$answers = array();

		foreach ($this->types as $key => $type) {

			$question = $this->matchQuestionsByType($text, $type, true);

			if (empty($question)) {
				continue;
			}
		
			$typeAnswers = QuestionImportFactory::create($this->target, $this->importType, $key)->saveAnswers($answer, true);

			$answers = array_merge($answers, $typeAnswers);
		}
		return $answers;
	}

	private function checkCharset($text)
	{
		$text = trim($text);
		if (mb_detect_encoding($text, array('UTF-8')) == 'UTF-8') {
			return $text;
		}
		return mb_convert_encoding($text, "UTF-8", "GBK, UTF-8, EUC-JP, UCS-2LE, JIS, eucjp-win, sjis-win");
	}

	private function matchQuestionsByType($text, $type, $isSub = false)
	{
		$types = $this->types;
		$key = array_search($type, $this->types);
		unset($types[$key]);
		$typesStr = implode('|', $types);

		if ($isSub) {
			preg_match_all("#{$type}[\s\S]*?({$typesStr}|【材料题结束】|【题型结束】|【导入结束】)#u", $text, $matches);
		} else {
			preg_match_all("#{$type}[\s\S]*?({$typesStr}|【材料题结束】|【题型结束】|【导入结束】)#u", $text, $matches);
		}

		$questionText = '';
		if (!empty($matches[0])) {
			foreach ($matches[0] as $match) {
				$questionText .= $match;
			}
		}

		$questionText = str_replace($this->types, '', $questionText);
		if ($isSub) {
			$questionText = str_replace('【材料题结束】', '', $questionText);
			$questionText = str_replace('【题型结束】', '', $questionText);
		} else {
			$questionText = str_replace('【导入结束】', '', $questionText);
		}
		return $questionText;
	}



}